/**
 * @author zhoushengtao
 * @since 2012-9-2 下午11:01:33
 */

package com.stchou.superbrowser.db;

import com.stchou.superbrowser.db.bean.website;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class WebsiteOpenHelper extends SQLiteOpenHelper
{
    private SQLiteDatabase SQLdb;
    private Cursor cursor;
    public final static String DB_NAME = "website"; // 数据库表名称
    public final static int DB_VERSION = 1;

    /**
     * @param context
     * @param name
     * @param factory
     * @param version
     */
    public WebsiteOpenHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

        db.execSQL("CREATE TABLE IF NOT EXISTS " + DB_NAME + " (" + website.DB_WEBSITE + " varchar(64), " + website.DB_WEBSITE_NAME + " varchar(64), "
                + website.DB_DATE + " varchar(128))");

        ContentValues values = new ContentValues();
        values.put(website.DB_WEBSITE, "www");
        values.put(website.DB_WEBSITE_NAME, "www");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, "wap");
        values.put(website.DB_WEBSITE_NAME, "wap");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, "http://m.baidu.com/");
        values.put(website.DB_WEBSITE_NAME, "百度首页");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, ".org");
        values.put(website.DB_WEBSITE_NAME, ".org");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, ".com");
        values.put(website.DB_WEBSITE_NAME, ".com");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, ".org");
        values.put(website.DB_WEBSITE_NAME, ".org");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);
        values.put(website.DB_WEBSITE, ".net");
        values.put(website.DB_WEBSITE_NAME, ".net");
        values.put(website.DB_DATE, System.currentTimeMillis() + "");
        db.insert(DB_NAME, null, values);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
        onCreate(db);
    }

    public List<website> query(String name)
    {
        List<website> list;

        /* 创建表 */
        SQLdb = getWritableDatabase(); // 调用SQLiteHelper.OnCreate()

        /* 查询表，得到cursor对象 *///
        cursor = SQLdb.query(DB_NAME, null, website.DB_WEBSITE + " like '" + name + "%'", null, null,
                null, website.DB_DATE + " DESC");

        list = new ArrayList<website>();
        list.clear();

        cursor.moveToFirst();
        while (!cursor.isAfterLast()
                && (cursor.getString(1) != null))
        {
            website site = new website();

            site.setWebsite(cursor.getString(0));
            site.setName(cursor.getString(1));
            site.setDate(cursor.getString(2));
            list.add(site);
            cursor.moveToNext();
        }
        return list;
    }

    public void close()
    {
        SQLdb.close();
    }
}
